package Control;


import Database.DBProduct;
import Model.Product;

public class ProductCtrl {
    Product productModel;

    DBProduct product = new DBProduct();

    public String createProduct(int productID, String name, double purchasePrice, double salesPrice, String countryOfOrigin, int minStock, int supplierID) {
	String message = null;
	if (productID != 0 && name != null && purchasePrice != 0 && salesPrice != 0 && countryOfOrigin != null && minStock != 0 && supplierID != 0) {

	   // if (product.findProduct(productID).getProductID() != productID) {
		productModel = new Product(productID, name, purchasePrice, salesPrice, countryOfOrigin, minStock, supplierID);

		try {
		    product.insertProduct(productModel);
		    message = "Product: " + name + " was created succesfuly!";
		} catch (Exception e) {

		    e.printStackTrace();
		}

	    } else {
		// Sent a message to the user that the input was wrong
		message = "Product with this ID[" + productID + "] already exist!";
	    }
//	} else {
//	    message = "Please fill up all fields!";

	//}
	return message;
    }

    public Product findProduct(int id) {
	Product pro = null;
	String message = null;
	try {
	    pro = product.findProduct(id, true);
	    System.out.println(pro.getName());
	} catch (Exception e) {

	   System.out.println("Not found!");
}


	return pro;

    }

    public String updateProduct(int productID, String name, double purchasePrice, double salesPrice, String countryOfOrigin, int minStock, int supplierID) {
	String message = null;
	//if (product.findProduct(productID).getProductID() == productID) {
	    productModel = new Product(productID, name, purchasePrice, salesPrice, countryOfOrigin, minStock, supplierID);
	    product.updateProduct(productModel);
	    message = name + " was updated succesfuly!";
//	} else {
//
//	    message = "Product doesn't exist!";
//	}
	// We should either prompt for what information to update
	// or if we should force an update in all fields of the product
	return message;
    }

    public String deleteProduct(int productID) {

	String message = null;
	//if (product.findProduct(productID).getProductID() == productID) {
	   // String name = product.findProduct(productID).getName();
	    product.delete(productID);

	    message = " was deleted succesfuly!";
//	} else {
//
//	    message = "Product doesn't exist!";
//	}
	// Here we need a script that removes the information
	// from the database

	return message;
    }

}
